package com.itheima.mm.service.impl;

import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.itheima.mm.dto.PermissionMenu;
import com.itheima.mm.dto.PermissionPoint;
import com.itheima.mm.dto.PermissionPoints;
import com.itheima.mm.pojo.permission.PermissionsAndGroup;
import com.itheima.mm.mapper.PermissionMapper;

import com.itheima.mm.mapper.PermissionsMapper;
import com.itheima.mm.pojo.permission.*;
import com.itheima.mm.service.PermissionService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.time.LocalDateTime;
import java.util.ArrayList;
import java.util.List;

@Service
public class PermissionServiceImpl extends ServiceImpl <PermissionMapper, Permission> implements PermissionService {
    @Autowired
    public PermissionsMapper permissionSMapper;
    @Autowired
    public PermissionMapper permissionMapper;


    //修改code
    @Override
    public void updateCode(PermissionPoint permissionPoint) {
        if (permissionPoint.getIsPoint()) {
            //为true修改的是point中code信息
            Integer id = permissionPoint.getId();
            String code = permissionPoint.getCode();
            permissionMapper.updatePointCode(id, code);
        } else {
            Integer id = permissionPoint.getId();
            String code = permissionPoint.getCode();
            permissionMapper.updateMenuCode(id, code);
            //为false修改的是menu中code信息
        }

    }

    //查询菜单所有的数据
    @Override
    public List <PermissionMenu> selectlist() {
        LambdaQueryWrapper <Permission> wrapper1 = new LambdaQueryWrapper <>();
        wrapper1.isNull(Permission::getPid);
        List <Permission> list = permissionMapper.selectList(wrapper1);
        List <PermissionMenu> permissions = new ArrayList <>();
        for (Permission permission : list) {
            PermissionMenu permissionMenu = new PermissionMenu();
            permissionMenu.setId(permission.getId());
            permissionMenu.setTitle(permission.getName());
            permissionMenu.setCode(permissionMapper.seleteMenuCode(permission.getId()));
            //查询所有的childs
            LambdaQueryWrapper <Permission> wrapper2 = new LambdaQueryWrapper <>();
            wrapper2.eq(Permission::getPid, permission.getId());
            List <Permission> list1 = permissionMapper.selectList(wrapper2);
            List <PermissionPoints> permissionPoints = new ArrayList <>();
            //创建收集权限点的组  的 组 ,  然后放进组里
            if (list1 != null) {
                for (Permission permission1 : list1) {
                    PermissionPoints permissionPoints1 = new PermissionPoints();
                    permissionPoints1.setId(permission1.getId());
                    permissionPoints1.setPid(permission1.getPid());
                    permissionPoints1.setTitle(permission1.getName());
                    permissionPoints1.setCode(permissionMapper.seleteMenuCode(permission1.getId()));
                    //查询所有的point并放入
                    LambdaQueryWrapper <Permission> wrapper3 = new LambdaQueryWrapper <>();
                    wrapper3.eq(Permission::getPid, permission1.getId());
                    List <Permission> list2 = permissionMapper.selectList(wrapper2);
                    List <PermissionPoint> points = new ArrayList <>();
                    if (list2 != null) {
                        //创建权限点并把复制然后把权限点放进组里面
                        for (Permission permission2 : list2) {
                            PermissionPoint permissionPoint = new PermissionPoint();
                            permissionPoint.setId(permission2.getId());
                            permissionPoint.setPid(permission2.getPid());
                            permissionPoint.setTitle(permission2.getName());
                            permissionPoint.setCode(permissionMapper.seletePointCode(permission2.getId()));
                            points.add(permissionPoint);
                        }
                    }
                    permissionPoints1.setPoints(points);
                    permissionPoints.add(permissionPoints1);
                }

            }
            permissionMenu.setChilds(permissionPoints);
            permissions.add(permissionMenu);
        }
        return permissions;
    }

    @Override
    public String delete(Integer id) {
        //先做判断 层级
        Permission permission = permissionMapper.selectById(id);
        //先判断是否为一级目录
        if (permission.getPid() == null) {
            //先删三级目录再删二级目录最后删一级目录
            LambdaQueryWrapper <Permission> wrapper = new LambdaQueryWrapper <>();
            wrapper.eq(Permission::getPid, permission.getId());
            List <Permission> permissions = permissionMapper.selectList(wrapper);
            for (Permission permission1 : permissions) {
                DeleteMenuById(permission1.getId(), permission1);
            }
            permissionMapper.deleteById(id);
            permissionMapper.deleteMenu(id);
            //删除权限组的权限
            LambdaQueryWrapper <PermissionsAndGroup> wrapperaa = new LambdaQueryWrapper <>();
            wrapperaa.eq(PermissionsAndGroup::getPid, id);
            permissionSMapper.delete(wrapperaa);
            //结果返还
            return "删除成功";
        }
        //判断是否为二级目录
        if (permission.getType() == 1) {
            //如果为二级目录先删三级目录后删三级目录
            DeleteMenuById(id, permission);
            //结果返还
            return "删除成功";
        }
        //都不是则为三级目录
        //三级point根据id删除 然后删除pe_permission_point_extend
        DeletePointById(id);
        return "删除成功";
    }

    @Override
    public String add(PermissionPoint permissionPoint) {
        //判断是menu还是point
        if (permissionPoint.getIsPoint()) {
            Permission permission = new Permission();
            permission.setCreateTime(LocalDateTime.now());
            permission.setName(permissionPoint.getTitle());
            permission.setType(2);
            permission.setPid(permission.getPid());
            //先存入数据库才能拿到新id
            permissionMapper.insert(permission);
            Integer permissionId = permission.getId();
            //然后把code存入point表并拿出pointId;
            permissionMapper.addPoint(permissionId, permissionPoint.getCode());
            Integer pointId = permissionMapper.getPointId(permissionId);
            //把pointId存入
            permission.setPermissionPointExtendId(pointId);
            permissionMapper.updateById(permission);
            return "添加成功";
        }
        Permission permission = new Permission();
        permission.setCreateTime(LocalDateTime.now());
        permission.setName(permissionPoint.getTitle());
        permission.setType(1);
        //注意一级目录没有pid需判断
        if (permission.getPid() != null) {
            permission.setPid(permission.getPid());
        }
        //先存入数据库才能拿到新id
        permissionMapper.insert(permission);
        Integer permissionId = permission.getId();
        //然后把code存入point表并拿出pointId;
        permissionMapper.addMenu(permissionId, permissionPoint.getCode());
        Integer pointId = permissionMapper.getMenuId(permissionId);
        //把pointId存入
        permission.setPermissionMenuExtendId(pointId);
        permissionMapper.updateById(permission);
        return "添加成功";
    }

    @Override
    public PermissionPoint selectById(Integer id) {
        Permission permission = permissionMapper.selectById(id);
        PermissionPoint permissionPoint = new PermissionPoint();
        permissionPoint.setId(permission.getId());
        if (permission.getType() == 1) {
            permissionPoint.setIsPoint(false);
            permissionPoint.setCode(permissionMapper.seleteMenuCode(id));
        } else {
            permissionPoint.setIsPoint(true);
            permissionPoint.setCode(permissionMapper.seletePointCode(id));
        }
        if (permission.getPid() != null) {
            permissionPoint.setPid(permission.getPid());
        }
        permissionPoint.setTitle(permission.getName());


        return permissionPoint;
    }

    private void DeleteMenuById(Integer id, Permission permission) {
        LambdaQueryWrapper <Permission> wrapper = new LambdaQueryWrapper <>();
        wrapper.eq(Permission::getPid, permission.getId());
        List <Permission> permissions = permissionMapper.selectList(wrapper);
        if (permissions != null) {
            for (Permission permission1 : permissions) {
                DeletePointById(permission1.getId());
            }
        }
        permissionMapper.deleteById(id);
        permissionMapper.deleteMenu(id);
        //删除权限组的权限
        LambdaQueryWrapper <PermissionsAndGroup> wrapperaa = new LambdaQueryWrapper <>();
        wrapperaa.eq(PermissionsAndGroup::getPid, id);
        permissionSMapper.delete(wrapperaa);
    }

    private void DeletePointById(Integer id) {
        permissionMapper.deleteById(id);
        permissionMapper.deletePoint(id);
        //删除权限组的权限
        LambdaQueryWrapper <PermissionsAndGroup> wrapper = new LambdaQueryWrapper <>();
        wrapper.eq(PermissionsAndGroup::getPid, id);
        permissionSMapper.delete(wrapper);

    }

}
